﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using RpgLibrary.SkillClasses;
using RpgLibrary;

namespace RpgEditor
{
    public partial class FormSkillDetails : FormDetailBase, IDetailForm
    {
       
        #region Field Region
        
        SkillData skill;
        
        #endregion
        
        #region Property Region
        
        public SkillData Skill
        {
            get { return skill; }
            set { skill = value; }
        }

        public override IRpgDataObject DataObject
        {
            get
            {
                return (IRpgDataObject)skill;
            }
            set
            {
                skill = (SkillData)value;
            }
        }

        #endregion

        public FormSkillDetails()
        {
            InitializeComponent();

            SetDataObjectValueFunction = SetSkillValue;
            LoadDataObjectValueFunction = LoadSkillValue;
        }

        #region Form Event Handler Region
        
        void LoadSkillValue()
        {
            if (Skill != null)
            {
                tbName.Text = Skill.Name;
                switch (Skill.PrimaryAttribute.ToLower())
                {
                    case "strength":
                        rbStrength.Checked = true;
                        break;
                    case "dexterity":
                        rbDexterity.Checked = true;
                        break;
                    case "cunning":
                        rbCunning.Checked = true;
                        break;
                    case "willpower":
                        rbWillpower.Checked = true;
                        break;
                    case "magic":
                        rbMagic.Checked = true;
                        break;
                    case "constitution":
                        rbConstitution.Checked = true;
                        break;
                }

                foreach (string s in Skill.ClassModifiers.Keys)
                {
                    string data = String.Format("{0}, {1}", s, Skill.ClassModifiers[s]);
                    lbModifiers.Items.Add(data);
                }
            }
        }
        
        bool SetSkillValue()
        {
            if (string.IsNullOrEmpty(tbName.Text))
            {
                MessageBox.Show("You must provide a name for the skill.");
                return false;
            }

            skill = new SkillData();
            skill.Name = tbName.Text;
            skill.PrimaryAttribute = GetCheckedRadioButton().Text;

            return true;
        }

        private RadioButton GetCheckedRadioButton()
        {
            foreach (Control control in gpAttribute.Controls)
                if ( ((RadioButton)control).Checked )
                    return (RadioButton)control;

            throw new Exception("Something is wrong-at least one radio button should be checked");
        }

        #endregion
    }
}
